import axios from 'axios'
import config from '../config/config'
import { Message } from 'element-ui'
const $axios = axios.create({
  baseURL: config.baseUrl,
  timeout: 1000 * 60 * 30
})

$axios.interceptors.request.use(
  config => {
    return config
  },
  error => {
    Message.error('网络请求失败！')
    console.error(error)
  }
)

$axios.interceptors.response.use(
  config => {
    return config.data
  },
  error => {
    Message.error('网络请求失败！')
    console.error(error)
  }
)

export default ({ url, method = 'GET', params, headers, data }) => {
  try {
    return $axios({
      url,
      method,
      params,
      headers,
      data
    })
  } catch (error) {
    console.error(error)
    throw new Error('请求出错')
  }
}
